package com.excel.easypoi;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/excel")
public class EasyPoiController {

    @Autowired
    private DeviceService deviceService;

    @GetMapping("/export")
    public void download(HttpServletResponse httpResponse) {
        ExcelCommon.exportExcel(deviceService.list(null),
                "设备列表" , "设备列表",ResourceDeviceEntity.class,"deviceResource.xls",
                httpResponse);
    }

    @GetMapping("/template")
    public void template(HttpServletResponse httpResponse) {
        ExcelCommon.exportExcel(new ArrayList<>(1),
                "设备列表" , "设备列表",ResourceDeviceEntity.class,"deviceResource.xls",
                httpResponse);
    }

    @PostMapping("/import")
    public ResResult importExcel(@RequestParam("file") MultipartFile file){
        List<ResourceDeviceEntity> resourceDeviceEntities = ExcelCommon.importExcel(file, 1, 1, ResourceDeviceEntity.class);
        deviceService.saveBatch(resourceDeviceEntities);
        return new ResResult<>().ok(resourceDeviceEntities.size());
    }
}
